package com.espirit.eap.sa;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.Table;

import com.espirit.eap.ICheckModelDuplicateInfo;
import com.espirit.eap.annotation.ModelDisplayProperty;
import com.googlecode.cswish.security.auth.UserInfo;

@Entity
@DiscriminatorValue("1")
@ModelDisplayProperty("name")
@Table(indexes={@Index(name="idx_saas_usertemplate_namepwd", columnNames={"name", "password"} )}, appliesTo = "saas_usertemplate")
@BatchSize(size=10)
//@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class LoginUser extends UserTemplate implements UserInfo,ICheckModelDuplicateInfo {

	private static final long serialVersionUID = 950718870587019925L;

	public static final String SUPER_ADMIN = "admin";
	
	public static final Integer STATUS_NORMAL = 1;
	// 状态设为无效，则不可以采用用户名、密码的方式登录，参见LoginUserService.login
	public static final Integer STATUS_INVALID = 2;
	
	private String password;
	
	private String email;
	
	// user status
	private Integer status;
	
	// transient property
	private Object userEx;
	
	// transient property
	private String[] roleIds;
	
	// FIXME: what's the purpose? transient property
	private LoginUser users;
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Column(length=50)
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@Transient
	public Object getUserEx() {
		return userEx;
	}
	
	public void setUserEx(Object userEx) {
		this.userEx = userEx;
	}
	
	@Transient
	public LoginUser getUsers() {
		return users;
	}

	public void setUsers(LoginUser users) {
		this.users = users;
	}

	@Transient
	@Override
	public String getLoginId() {
		return String.valueOf(getId());
	}
	
	@Transient
	@Override
	public String[] getRoleIds() {
		return this.roleIds;
	}

	public void setRoleIds(String[] roleIds) {
		this.roleIds = roleIds;
	}

	private final static String[] gatherProperties = new String[]{"name"};
	@Override
	public String[] gatherProperties() {
		return gatherProperties;
	}
	
	@Transient
	public boolean isSuperAdmin() {
		return LoginUser.SUPER_ADMIN.equals(this.getName());
	}
}